package com.gmrz.uaf.protocol.v1.processor;

import com.gmrz.uaf.db.DAOException;
import com.gmrz.uaf.db.UAFDAOFactory;
import com.gmrz.uaf.db.UAFDBConnectionMgr;
import com.gmrz.uaf.db.dao.AuthenticatorDAO;
import com.gmrz.uaf.protocol.v1.processor.exception.UAFErrorCode;
import com.gmrz.util.db.DBUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.sql.Connection;
import java.sql.SQLException;

/**
 * @PACKAGE_NAME: com.gmrz.uaf.protocol.v1.processor
 * @ClassName: UAFDeviceCostedProcessor
 * @Description: 计费设备查询 Processor
 * @Date: 2020-04-28 18:20
 * @Author: liyl@gmrz-bj.com
 **/
public class UAFDeviceCostedProcessor extends UAFBaseProcessor {

    private static final Logger LOG = LogManager.getLogger(UAFDeviceCostedProcessor.class);

    public int deviceCostedSearch(String deviceID, String securityLevel) throws DAOException {
        Connection conn = null;
        try {
            conn = UAFDBConnectionMgr.getConnection(true);
            AuthenticatorDAO authenticatorDAO = UAFDAOFactory.createAuthenticatorDAO(conn);
            int isCosted = authenticatorDAO.selectCertCountByDeviceIDAndSecurityLevel(deviceID, securityLevel);
            return isCosted;

        }catch (SQLException e){
            LOG.error("Failed to access t_certificate data, ", e);
            throw new DAOException(UAFErrorCode.DB_DATABASE_CONNECTION_ERROR, e);
        }finally {
            DBUtil.close(conn);
        }
    }

}
